﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Agents.Agent
{
    //This page allows the agent to see all clients in a dataGrid 
    //and then choose who he whould like to visit next.
    public partial class ChooseCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //This function is activated when an agent chooses a client to visit from the data grid
        protected void DataGridProjects_EditCommand1(object source, DataGridCommandEventArgs e)
        {
            Queries queries = new Queries();

            //retrieving the client from the firs cell in e (e is the row that was selected)
            int clientId = Int32.Parse(e.Item.Cells[0].Text);
            Clients client = queries.GetClient(clientId);

            // Get the agent who is in the page
            Agents agent = queries.GetAgentByUserName(Page.User.Identity.Name);

            // Update that the agent is going to this client
            queries.ChangeGoingTo(agent, client);

            //redirecting to the Agents page (a special page for while he is deaking with a client)
            Response.Redirect("~/Agent/StartShiftDisplay.aspx?clientId=" + clientId);
        }

        //function is activated when a new datagrid page is selected
        protected void DataGridProjects_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            //saving the index chosen
            int index = e.NewPageIndex;
            //moving to the selected page.
            DataGridProjects.CurrentPageIndex = index;
            DataGridProjects.DataBind();
        }

        protected void backBtn_Click(object sender, EventArgs e)
        {
            //going back to the agents home page without doing anything.
            Response.Redirect("~/Agent/AgentMenu.aspx");
        }

        /// <summary>
        /// Occurs when the user filters or unfilters the results by
        /// the customer's name, and updates results.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            // Filter
            if ((btnFilter.Text == "סנן") && (!String.IsNullOrEmpty(txtCustomerName.Text)))
            {
                SqlDataSource1.FilterExpression =
                    String.Format("Name LIKE '%{0}%'", txtCustomerName.Text);
                btnFilter.Text = "בטל סינון";
            }
            // Unfilter
            else
            {
                SqlDataSource1.FilterExpression = null;
                btnFilter.Text = "סנן";
            }
        }
    }
}